package com.cskaoyan.market.service.wx.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.cskaoyan.market.config.AliyunConfig;
import com.cskaoyan.market.service.wx.CloudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.UUID;

/**
 * @ClassName AliyunService
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/5/22 15:17
 * @Version V1.0
 **/
@Service
public class AliyunService implements CloudService {

    @Autowired
    AliyunConfig aliyunConfig;

    @Override
    public void sendSms(String phoneNumber, String code) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
        //再利用信息类创建出发送短信的客户端类
        IAcsClient client = new DefaultAcsClient(profile);
        //便是发送短信的请求封装体
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        //只需要按照官方文档的提示去构建这些信息即可
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", aliyunConfig.getMail().getSignName());
        request.putQueryParameter("TemplateCode", aliyunConfig.getMail().getTemplateCode());
        request.putQueryParameter("TemplateParam", "{\"code\":\""+ code +"\"}");
        try {
            //这一步便是发送短信的请求，随后返回响应
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (com.aliyuncs.exceptions.ClientException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void upload(String filename, File file) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        //设定存储文件的资源类型 MIME类型
        objectMetadata.setContentType("image/jpg");
        OSS ossClient = new OSSClientBuilder().build(aliyunConfig.getOss().getEndPoint(), aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
        String fileName = UUID.randomUUID().toString().replaceAll("-","") + ".png";
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //再利用oss给我们封装的方法来执行文件上传即可
        PutObjectResult putObjectResult = ossClient.putObject(aliyunConfig.getOss().getBucketName(), fileName, file,objectMetadata);
        System.out.println(putObjectResult);
        ossClient.shutdown();
    }

    @Override
    public String upload(String filename, InputStream inputStream) {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        //设定存储文件的资源类型 MIME类型
        objectMetadata.setContentType("image/jpg");
        OSS ossClient = new OSSClientBuilder().build(aliyunConfig.getOss().getEndPoint(), aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret());
        String fileName = UUID.randomUUID().toString().replaceAll("-","") + ".png";
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        //再利用oss给我们封装的方法来执行文件上传即可
        PutObjectResult putObjectResult = ossClient.putObject(aliyunConfig.getOss().getBucketName(), fileName, inputStream,objectMetadata);
        System.out.println(putObjectResult);
        ossClient.shutdown();

        return fileName;
    }
}
